package com.crm.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.PageResult;
import com.crm.domain.dto.RoleDto;
import com.crm.domain.entity.Role;
import com.crm.domain.vo.RoleVo;
import com.crm.mapper.RoleMapper;
import com.crm.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private final RoleMapper roleMapper;

    @Override
    public PageResult<Role> getPagedUsers(Integer pageNum, Integer pageSize) {
        Integer total = getTotalRoles();
        List<Role> users = getRoleList(pageNum, pageSize);

        return new PageResult<>(200, "查询成功", users, total);
    }

    @Override
    public List<RoleVo> select(RoleDto role) {
      List<Role> roles =  roleMapper.select();
        return BeanUtil.copyToList(roles, RoleVo.class);
    }


    private List<Role> getRoleList(Integer pageNum, Integer pageSize) {
        Integer offset = (pageNum - 1) * pageSize;
        List<Role> users = roleMapper.selectUserByPage(offset, pageSize);
        return users;
    }

    private Integer getTotalRoles() {
        return roleMapper.countUsers();
    }
}
